package com.csi.bumblebee.service.impl;

import com.csi.bumblebee.domain.user.MyCollection;
import com.csi.bumblebee.domain.user.MyJob;
import com.csi.bumblebee.domain.vo.MyJobReturnVo;
import com.csi.bumblebee.mapper.JobMapper;
import com.csi.bumblebee.service.JobService;
import com.csi.bumblebee.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import java.util.List;

/**
 * job 表
 *
 * @author yeebs
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class JobServiceImpl implements JobService {

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Autowired
    private JobMapper jobMapper;

    /**
     * 增加岗位
     *
     * @param job job对象
     * @return sql影响行数
     */
    @Override
    public Boolean add(MyJob job) {

        //设置id
        job.setId(IdUtil.getString("UJ"));

//        // 放置redis
        if (jobMapper.add(job)) {
            redisPut(job);
            return true;
        }
        return false;
    }


    /**
     * 查询岗位信息详情页
     *
     * @param jobId 岗位id
     * @return job对象
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public MyJob findByIdJob(String jobId) {

        return this.jobMapper.findByIdJob(jobId);

    }

    /**
     * 放入redis
     *
     * @param job 岗位对象
     */
    private void redisPut(MyJob job) {
        redisTemplate.opsForValue().set(job.getId(), job, getExpireTime());
    }

    /**
     * 从redis取数据
     *
     * @param id job id
     * @return job对象
     */
    private MyJob redisGet(String id) {
        redisTemplate.expire(id, getExpireTime(), TimeUnit.SECONDS);
        return (MyJob) redisTemplate.opsForValue().get(id);
    }

    /**
     * 从redis删除
     *
     * @param id company id
     * @return 删除的company
     */
    private MyJob redisDelete(String id) {
        MyJob job = redisGet(id);
        redisTemplate.delete(id);
        return job;
    }

    private long getExpireTime() {
        return 60 * 60 * 24 * 7;
    }

    /**
     * 收藏岗位
     *
     * @param myCollection 岗位收藏夹对象
     * @return 影响行数
     */
    @Override
    public int likeJob(MyCollection myCollection) {

        if (myCollection.getJobId() != null
                && myCollection.getStudentId() != null) {
            List<MyCollection> likeJob = findLikeJob(myCollection);
            if (likeJob != null && likeJob.size() == 0) {
                //创建自定义ID
                myCollection.setId(IdUtil.getString("CT"));
                return this.jobMapper.likeJob(myCollection);
            } else {
                System.out.println("数据库已存在收藏");
                return 0;
            }
        }

        return 0;

    }

    /**
     * 取消收藏岗位
     *
     * @param myCollection 岗位收藏夹对象
     * @return 影响行数
     */
    @Override
    public int deleteLikeJob(MyCollection myCollection) {

        return this.jobMapper.deleteLikeJob(myCollection);
    }

    /**
     * 查询是否已收藏记录
     *
     * @param myCollection 收藏岗位夹对象
     * @return myCollection集合长度
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<MyCollection> findLikeJob(MyCollection myCollection) {

        if (myCollection.getJobId() != null
                && myCollection.getStudentId() != null) {
            System.out.println(this.jobMapper.findLikeJob(myCollection));
            return this.jobMapper.findLikeJob(myCollection);


        }

        return null;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<MyJobReturnVo> favouriteList(String studentId) {
        return jobMapper.favouriteList(studentId);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public PageInfo<MyJobReturnVo> selectList(MyJobReturnVo myJobReturnVo) {

        // 获取搜索内容
        String name;
        if (myJobReturnVo != null) {
            name = myJobReturnVo.getSearchName();
            myJobReturnVo.setSearchNames(new ArrayList<>());

            if("".equals(name)) {
                name = null;
            }

            if (name != null) {
                int num = 0;
                for (int i = name.length(); i > 0; i--) {
                    for (int j = 0; j <= name.length() - i; j++) {
                        myJobReturnVo.getSearchNames().add(name.substring(j, j + i));
                    }
                }
            } else {
                myJobReturnVo.getSearchNames().add("");
            }

            PageHelper.startPage(myJobReturnVo.getPageNum(), myJobReturnVo.getPageSize());
            return new PageInfo<>(jobMapper.selectList(myJobReturnVo));
        }
        return null;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<MyJobReturnVo> listByType(String jobTypeId) {
        return jobMapper.listByType(jobTypeId);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<MyJobReturnVo> listByTime() {
        return jobMapper.listByTime();
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public PageInfo<MyJobReturnVo> collectionListByStudent(String studentId, int pageNum) {
        PageHelper.startPage(pageNum, 10);
        return new PageInfo<>(jobMapper.collectionListByStudent(studentId));
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public PageInfo<MyJobReturnVo> submittedListByStudent(String studentId, int pageNum) {
        PageHelper.startPage(pageNum, 10);
        return new PageInfo<>(jobMapper.submittedListByStudent(studentId));
    }


    /**
     * 查询一个二级岗位分类下的所有已发出的岗位列表
     * @param jobTypeId 岗位二级分类id
     * @return 岗位集合
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<MyJob> listJobTwos(String jobTypeId) {

        if (jobTypeId != null && !("".equals(jobTypeId))){

            return this.jobMapper.listJobTwos(jobTypeId);

        }

        return null;

    }


}
